package com.byinsight.web.actions;

public class WebException extends RuntimeException {
	private static final long serialVersionUID = 4901105960687418485L;
	private int code;
	private String msg;
	private Throwable m_root;
	
	@Deprecated
	//better not use it - all WebException must have a predefined code, which is registered in ErrorCode enum
	public WebException() {	
	}

	public WebException(ErrorCode errorCode) {
		this.code = errorCode.ordinal();
		if (code >= 100) {
			// 100 represents successful.
			code += 1;
		}
		this.msg = errorCode.getDesc();
	}
	
	public WebException(ErrorCode errorCode, Throwable t) {
		this(errorCode);
		m_root = t;
	}
	
	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getStackTraceString() {
		Throwable root = this;
		if (m_root != null) {
			root = m_root;
		}
		return getStackTraceString(root);
	}
	
	private String getStackTraceString(Throwable t) {
		StackTraceElement[] traces = t.getStackTrace();
		StringBuffer sb = new StringBuffer();
		
		sb.append(t.toString());
		for (StackTraceElement e : traces) {
			sb.append("\r\n\t");
			sb.append(e);
			
		}
		return sb.toString();
	}

}
